#include "remoteselector.h"

/* 初始化本地蓝牙 */
remoteselector::remoteselector(QBluetoothAddress &localAdapter, QObject *parent): QObject(parent)
{
    // 初始化一个用于执行蓝牙服务发现的代理对象
    m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(localAdapter);
    connect(m_discoveryAgent,SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
    connect(m_discoveryAgent, SIGNAL(finished()), this,SLOT(discoveryFinished()));
    connect(m_discoveryAgent, SIGNAL(canceled()), this,SLOT(discoveryFinished()));
}

remoteselector::~remoteselector()
{
    delete m_discoveryAgent;
}

/* 开启发现模式，这里无需设置过滤 uuid，否则搜索不到手机
 * uuid 会过滤符合条件的 uuid 服务都会返回相应的蓝牙设备
*/

void remoteselector::startDiscovery(const QBluetoothUuid &uuid)
{
    Q_UNUSED(uuid);
    qDebug()<<"remoteselector::startDiscovery";
    if (m_discoveryAgent->isActive()) {
        qDebug()<<"stop the searching first";
        m_discoveryAgent->stop();
    }

    //m_discoveryAgent->setUuidFilter(uuid);

    // 开始执行完整的蓝牙服务发现过程
    m_discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
}

/* 停止发现 */
void remoteselector::stopDiscovery()
{
    qDebug()<<"remoteselector::stopDiscovery";
    if (m_discoveryAgent){
        m_discoveryAgent->stop();
    }
}

/* 返回服务信息 */
QBluetoothServiceInfo remoteselector::service() const
{
    qDebug()<<"remoteselector::service";
    return m_serviceInfo;
}

/* 扫描蓝牙服务信息[slot] */
void remoteselector::serviceDiscovered(const QBluetoothServiceInfo&serviceInfo)
{
#if 0
    qDebug() << "Discovered service on"<< serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"<< serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"<< serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"<< serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
#endif

    qDebug()<<"remoteselector::serviceDiscovered";

    /*
        这段代码使用 QMapIterator 遍历名为 m_discoveredServices 的 QMap 对象。
        它迭代并检查其中的每个元素。如果 serviceInfo 中的设备地址与已发现服务中的某个元素的设备地址相匹配，
        那么代码会直接返回，不再执行后续操作。这段代码的目的可能是避免重复添加相同的服务。
    */
    QMapIterator<QString, QBluetoothServiceInfo>i(m_discoveredServices);
    while (i.hasNext()){
        i.next();
        if (serviceInfo.device().address() == i.value().device().address()) {
            return;
        }
    }

    QString remoteName;
    if (serviceInfo.device().name().isEmpty())
        remoteName = serviceInfo.device().address().toString();
    else
        remoteName = serviceInfo.device().name();

    qDebug()<<"adding to the list....";
    qDebug()<<"remoteName: "<< remoteName;

    QListWidgetItem *item = new QListWidgetItem(QString::fromLatin1("%1%2").arg(remoteName,serviceInfo.serviceName()));
    m_discoveredServices.insert(remoteName, serviceInfo);
    emit sig_newServiceFound(item);
}

/* 发现完成 */
void remoteselector::discoveryFinished()
{
    qDebug()<<"remoteselector::discoveryFinished";
    emit finished();
}


